(function(window){
	var u = {};
	
	/**
     * table交互声明
     * @type {{}}
     */
    var vues = {};//map
	
	u.addTree = function(url, id, func, isLoading, loadingId){
		if(isLoading){
			$(loadingId).mLoading("show");
		}
		var elSel = "div[_tree=" + id.substr(1) + "]";
		var oldUrl = url;
		var el = $(elSel).get(0);
		var data = {
			page: null,
			size: 10
		};
		var vue = new Vue({
			el: el,
			data: data,
			methods: {
				/**
                 * 设置数据
                 * @param obj
                 */
                setPage: function (obj) {
                	if(obj){
                		var level = null;
                		var arr = [];
                		for(var i = 0; i< obj.length; i++) {
                			var data = obj[i];
                			level = data.level;
                			var clazz = null;
                			arr[level] = data.guid;
                			if (level> 1) {
                				var list = arr.slice(1,level)
                				clazz = list.join(" ");
                			}
                			
                			if(clazz){
                				data.clazz = clazz;
                				obj[i] = data;
                			}
                		}
                	}
                    this.page = obj;
                },
                expand: function(item,e){
                	e.stopPropagation();
                	var el = e.target;
                	var elTr = "li[data-tt-parent-id=" + item.guid + "]";
                	var clazz = $(el).attr('class');
                	if(clazz.indexOf("fa-folder-open-o")>0) {
    	    			$(el).removeClass("fa-folder-open-o").addClass("fa-folder");
    	    			$("."+item.guid+":not(#"+item.guid+")").hide();
    	    			$("."+item.guid+":not(#"+item.guid+")").find(".fa-folder-open-o").removeClass("fa-folder-open-o").addClass("fa-folder");
    	    		}else if(clazz.indexOf("fa-folder")>0){
    	    			$(el).removeClass("fa-folder");
    	    			$(el).addClass("fa-folder-open-o");
     	    			$(elTr).show();
    	    		}
                	
                },
                refreh: function(){
                	this.resetUrl();
                },
                /**
                 * 还原初始Url
                 */
                resetUrl: function () {
                    url = oldUrl;
                    loadData();
                },
                Icheck: function (item,e) {
                	var el = e.target;
                	var check = $(el).is(':checked');
                	if(check){
                		if (item.clazz) {
                			var arrays = item.clazz.split(" ");
                			for(var i=0 ; i<arrays.length; i++) {
                				$("#" +arrays[i] + " div input:checkbox").prop("checked",true);
                			}
                		}
                		$("." +item.guid + " div input:checkbox").prop("checked",true);
                	} else {
                		$("." +item.guid + " div input:checkbox").prop("checked",false);
                	}
                	
                },
                Iselected: function(){
                	var list = $(":checked");
                	var select = [];
                	if(list){
                		for(var i=0 ; i<list.length; i++) {
                			alert(list(i).value());
                			select.push(list(i).value());
                		}
                	}
                },
                selectItem:func
			}
		});
		
		vues[id] = vue;//table传递
        /**
         * 加载数据
         * @param pageNo
         * @param pageSize
         */
        function loadData() {
            if (url) {
                $byking.getJSONWithToken(url, function (data) {
                    vue.setPage(data.data);
                    if(isLoading){
                     	$(loadingId).mLoading("hide");
                     }
                },function(data){
                	layer.msg(data.responseJSON.message, {icon:5});
                	if(data.responseJSON.code == "9001"){
                		window.location.href = $hostconfig.rootpath+"/page/login.html";
                	}
                });
            }
        }
        
		loadData();
	};
	
	/**
     * 获取某个Vue，用于页面交互
     * @param id tableId
     * @returns {*}
     */
    u.getVue = function (id) {
        return vues[id];
    };
    
    /**
     * setVue
     * @param id
     * @param vue
     */
    u.setVue = function (id, vue) {
        vues[id] = vue;
    };
	
	window.$tree = u;
})(window)